package com.example.selfservicestation.singleton

import android.content.Context
import android.content.SharedPreferences
import android.content.SharedPreferences.Editor
import com.example.selfservicestation.MyApplication

object SharedPreferenceUtil {
    private val mPreferences: SharedPreferences
    private val mEditor: Editor
    private val mTAG = "DebugTool"
    init {
        mPreferences = MyApplication.instance.getSharedPreferences(mTAG, Context.MODE_PRIVATE)
        mEditor = mPreferences.edit()
    }

    // 存入数据
    fun putString(key: String, value: String) {
        mEditor.putString(key, value)
        mEditor.commit()
    }

    fun putInt(key: String, value: Int){
        mEditor.putInt(key, value)
        mEditor.commit()
    }

    fun putBoolean(key: String, value: Boolean){
        mEditor.putBoolean(key, value)
        mEditor.commit()
    }

    fun getString(key: String, defaultValue: String = ""): String {
        return mPreferences.getString(key, defaultValue)?: ""
    }

    fun getInt(key: String, defaultValue: Int = 0): Int {
        return mPreferences.getInt(key,defaultValue)
    }

    fun getBoolean(key: String, defaultValue: Boolean = false): Boolean {
        return mPreferences.getBoolean(key,defaultValue)
    }

    // 移除数据
    fun removeSP(key: String) {
        mEditor.remove(key)
        mEditor.commit()
    }
}